function Search(input, for_compare) {
    var input          = $(input);
    var search_block   = input.closest('.search-block');
    var search_results = search_block.find('.search-results');
    var for_compare    = for_compare || false;
    //var timeout        = 700;
    
    this.init = function () {
        var _this = this;
        input.keydown(function () {
            _this.search();
            
            // TODO
            /*if (timeout > 0) {
                timeout = 700;
                timeout_id = window.setTimeout('timeout -= 100', 100); 
                console.log('skipping' + timeout);   
            } else {
                //
                console.log('search');   
            } */
        }); 
        document.body.addEventListener('click', function () {
            _hide_results();  
        });   
    }
    
    this.search = function () {         
        var query   = input.val();
        _search(query);    
    }   
    
    var _search = function(query) {
        var query = $.trim(query);
        if (query != '') {
            $.post('/index/index/search_ajax', {query: query}, function (data) {
                if (data.success) {
                    _show_results(data.data);
                    if (for_compare) {
                        _click_select();    
                    } else {
                        _click_redirect();   
                    }  
                }        
            }, 'json');  
        }    
    }
    
    var _show_results = function(data) {
        if (data) {
            search_results.html(data).show();
        }    
    }
    
    var _hide_results = function() {
        search_results.html('').hide();  
    }
    
    var _click_redirect = function() {
        search_results.find('.search-items ul li').click(function () {
            var href = $(this).attr('data-href');
            document.location = href;
        });    
    }
    
    var _click_select = function() {
        search_results.find('.search-items ul li').click(function () {
            var id = $(this).attr('id');
            var name = $(this).attr('data-name'); 
            search_block.find('input[name ^= model]').val(id).trigger('change');
            input.val(name);
        });    
    }
    
}

function SearchCompare(input1, input2) {
    var search1 = new Search(input1, true);
    var search2 = new Search(input2, true);
    var form    = $(input1).closest('form');
    
    var hidden1 = form.find('input[name = model-1]');
    var hidden2 = form.find('input[name = model-2]');
    
    this.init = function () {
        search1.init();  
        search2.init();
        form.find('input[name ^= model]').change(function () {           
            if (hidden1.val() != 0 && hidden2.val() != 0) {
                form.find('input[type = submit]').attr('disabled', false);    
            }
        });
        form.submit(function () {
            if (hidden1.val() != 0 && hidden2.val() != 0) {
                return true;    
            }
            return false;    
        });  
    }
}

$(document).ready(function () {
    $('.product-image-slider').flexslider({
        direction: "vertical",
        controlNav: false,
        directionNav: true,
        pauseOnHover: true,
        slideshowSpeed: 10000      
    });
    
    $('span.hlink').each(function() {
        var link = $(this).attr('hlink');
        var html = $(this).html();
        
        var classes = $(this).removeClass('hlink').attr('class');
        var target  = $(this).attr('target');
        
        var parent = $(this).parent();
        var a = $('<a></a>').attr('href', link).html(html).addClass(classes);
        if (target) {
            a.attr('target', target);
        }
        
        $(this).replaceWith(a);
    })
}); 
